<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="160px">
      <el-form-item label="简称" prop="busMainData.cpyName">
        <el-input v-model="form.busMainData.cpyName" style="width: 300px;"/>
      </el-form-item>
      <el-form-item label="全称" prop="busMainData.cpyLongName">
        <el-input v-model="form.busMainData.cpyLongName" style="width: 300px;"/>
      </el-form-item>
      <el-form-item label="证件编号" prop="busMainData.cerCode">
        <el-input v-model="form.busMainData.cerCode" style="width: 300px;"/>
      </el-form-item>
      <el-form-item label="公司类型" prop="busMainData.cpyType">
        <el-select v-model="form.busMainData.cpyType" :style="style" placeholder="请选类型">
          <el-option
            v-for="(item) in dicts.cpy_type"
            :key="item.id"
            :label="item.dictName"
            :value="item.dictCode"/>
        </el-select>
      </el-form-item>
      <el-form-item label="本位币" prop="busMainData.priceCur">
        <el-select v-model="form.busMainData.priceCur" :style="style" placeholder="请选本位币">
          <el-option
            v-for="(item) in dicts.bus_price_cur"
            :key="item.id"
            :label="item.dictName"
            :value="item.dictCode"/>
        </el-select>
      </el-form-item>
      <el-form-item label="财务账期起止日" prop="busMainData.finBeginDay">
        <template slot="label">
          财务账期起止日<IconHelp display-content="不填默认为自然月." />
        </template>
        <el-input-number v-model.number="form.busMainData.finBeginDay" :controls="false" :precision="0" :max="31" size="mini" style="width:80px;"/>
        -
        <el-input-number v-model.number="form.busMainData.finEndDay" :controls="false" :precision="0" :max="31" size="mini" style="width:80px;"/>
      </el-form-item>
      <el-form-item label="办公地址" prop="busMainData.officeAddress">
        <el-input v-model="form.busMainData.officeAddress" />
      </el-form-item>
      <el-form-item label="电话" prop="busMainData.telphone">
        <el-input v-model="form.busMainData.telphone" />
      </el-form-item>
      <el-form-item label="银行账号/名称" prop="busMainData.bankAccount">
        <el-input v-model="form.busMainData.bankAccount" style="width:120px;" />
        <el-input v-model="form.busMainData.bankName" style="width:120px;"/>
      </el-form-item>
      <el-form-item label="主要联系人/手机" prop="busMainData.mobile">
        <el-input v-model="form.busMainData.mainContacor" style="width:120px;" />
        <el-input v-model="form.busMainData.mobile" style="width:120px;" />
      </el-form-item>
      <el-form-item label="公司官网" prop="busMainData.cpyUrl">
        <el-input v-model="form.busMainData.cpyUrl" />
      </el-form-item>
      <el-form-item label="收货地址" prop="busMainData.recAddress">
        <el-input v-model="form.busMainData.recAddress" />
      </el-form-item>
      <el-form-item label="邮件地址" prop="busMainData.email">
        <el-input v-model="form.busMainData.email" />
      </el-form-item>
    </el-form>
    <div v-permission="['SYS_ADMIN','COMMON_ADMIN']" slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">重置</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </div>
</template>

<script>
import { doSaveInfo, getCurCpyInfo } from '@/api/system/cpy'

export default {
  components: { },
  props: {
  },
  dictTypes: ['cpy_status', 'enable_flag', 'cpy_type', 'bus_price_cur'],
  data() {
    return {
      isAdd: false,
      loading: false,
      dialog: false,
      style: 'width: 184px',
      form: {
        busMainData: {
          cpyName: '',
          cpyLongName: '',
          cerCode: '',
          cpyType: '1',
          cpyStatus: 'ONLINE',
          enableFlag: 'Y'
        }
      },
      rules: {
        'busMainData.cpyName': [
          { required: true, message: '请输入简称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        'busMainData.cpyLongName': [
          { required: true, message: '请输入全称', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ],
        'busMainData.cerCode': [
          { required: true, message: '请输入证件编号', trigger: 'blur' },
          { min: 15, max: 20, message: '长度在 15 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.viewMainData()
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    viewMainData() {
      getCurCpyInfo().then(res => {
        this.form = res.data
      })
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.doEdit()
        }
      })
    },
    doEdit() {
      doSaveInfo(this.form).then(res => {
        this.loading = false
        this.viewMainData()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      // this.dialog = false
      this.viewMainData()
    }
  }
}
</script>
